<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use app\admin\model\AdvisoryInfo;
use app\admin\model\AdvisoryPerson;
class Advisory extends Rbac
{
//    咨询部查询页面
    public function advisory()
    {
//        拿到权限session
//        session('u_r_id')
        $quanxian = $_SESSION['u_r_id'];

//        拿到名字
        $r_name= $_SESSION['u_name'];

//       咨询部主管或超管进入页面
//        开始时间俺的截止时间帅选
       if ($quanxian== 6||$quanxian== 1){
           $timeFirst = strtotime(request()->param('start'));
           $endTime = strtotime(request()->param('end'));
           $res = new AdvisoryInfo();
           if (!empty($timeFirst)&&!empty($endTime)) {

               $result = $res->whereTime('job_inter_time', 'between', [$timeFirst, $endTime])
                   ->where('job_inter_man!=""')
                   ->where('ISNULL(job_delete_time)')
                   ->paginate(10);

           }else {
               $result = $res->where('job_inter_man!=""')
                   ->where('ISNULL(job_delete_time)')
                   ->paginate(10);

           }
           $this->assign('result',$result);

           $res = Db::table('mdm_user')->field('user_name')->where('user_r_id in(6,7)')
               ->select();
//      拿取所有咨询师的名字
           $names=[];
           for ($i=0;$i<count($res);$i++){
               $names[]= $res[$i]['user_name'];
           }
//      联表去查个数
//      资讯次数
           $join_inter = 'u.user_name=j.job_inter_man and j.job_inter_time between '."'$timeFirst'".' and '."'$endTime'".' and j.job_delete_time IS NULL';
           $inter_num = Db::table('mdm_user')->alias('u')
               ->field('u.user_name,COUNT(j.job_id) as init_num')
               ->join('mdm_job j',$join_inter,'left')
               ->where('u.user_r_id in(6,7)')
               ->group('u.user_name')
               ->select();
//     试岗次数s
           $join_try = 'u.user_name=j.job_inter_man and j.job_inter_time between '."'$timeFirst'".' and '."'$endTime'".' and j.job_try_sn=1 and j.job_delete_time IS NULL';
           $try_num = Db::table('mdm_user')->alias('u')
               ->field('u.user_name,COUNT(j.job_id) as try_num')
               ->join('mdm_job j',$join_try,'left')
               ->where('u.user_r_id in(6,7)')
               ->group('u.user_name')
               ->select();
//      签单
           $join_order = 'u.user_name=j.job_inter_man and j.job_order_time between '."'$timeFirst'".' and '."'$endTime'".' and j.job_delete_time IS NULL';
           $order_num = Db::table('mdm_user')->alias('u')
               ->field('u.user_name,COUNT(j.job_id) as order_num')
               ->join('mdm_job j',$join_order,'left')
               ->where('u.user_r_id in(6,7)')
               ->group('u.user_name')
               ->select();

//      将拿到的数据组装成数组
           $inter = [];
           for($i = 0;$i < count($inter_num);$i++){
//               $user_name = $inter_num[$i]['user_name'];
               $inter[$i][] = $inter_num[$i];
               $inter[$i][] = $try_num[$i];
               $inter[$i][] = $order_num[$i];
           }
           $this->assign('inter',$inter);
//           var_dump($inter);
           return $this->fetch('advisory/connoisseur');
       }


//咨询部人员浏览内容权限判断
        if ($quanxian == 7|| $quanxian == 1 ||  $quanxian == 6){
    //            拿时间      通过时间筛选
                $timeFirst = strtotime(request()->param('start'));
                $endTime = strtotime(request()->param('end'));
    //      拿取该咨询师对应的数据
                $res = new AdvisoryInfo();
                $result = $res->whereTime('job_inter_time', 'between', [$timeFirst, $endTime])->where('job_inter_man',$r_name)
                    ->where('job_delete_time=""')
                    ->select();
                $this->assign('result',$result);

    //     通过月份筛选
    //        资讯次数
            $join_inter = 'u.user_name=j.job_inter_man and j.job_inter_time between '."'$timeFirst'".' and '."'$endTime'".' and j.job_delete_time IS NULL';
            $inter_num = Db::table('mdm_user')->alias('u')
                ->field('u.user_name,COUNT(j.job_id) as init_num')
                ->join('mdm_job j',$join_inter,'left')
                ->where('u.user_r_id in(6,7)')
                ->where('u.user_name',$r_name)
                ->select();
            //     试岗次数s
                $join_try = 'u.user_name=j.job_inter_man and j.job_inter_time between '."'$timeFirst'".' and '."'$endTime'".' and j.job_try_sn=1 and j.job_delete_time IS NULL';

                $try_num = Db::table('mdm_user')->alias('u')
                ->field('u.user_name,COUNT(j.job_id) as try_num')
                ->join('mdm_job j',$join_try,'left')
                ->where('u.user_r_id in(6,7)')
                ->where('u.user_name',$r_name)
                ->select();
            //      签单
            $join_order = 'u.user_name=j.job_inter_man and j.job_inter_time between '."'$timeFirst'".' and '."'$endTime'".' and j.job_try_sn=1 and j.job_order_time<>null and j.job_delete_time IS NULL';
            $order_num = Db::table('mdm_user')->alias('u')
                ->field('u.user_name,COUNT(j.job_id) as order_num')
                ->join('mdm_job j',$join_order,'left')
                ->where('u.user_r_id in(6,7)')
                ->where('u.user_name',$r_name)
                ->select();

            // 将拿到的数据组装成数组
                $inter = [];
                for($i = 0;$i < count($inter_num);$i++){
                    $inter[$i][] = $inter_num[$i];
                    $inter[$i][] = $try_num[$i];
                    $inter[$i][] = $order_num[$i];
                }
            $this->assign('inter',$inter);
            return $this->fetch('advisory/talk');
      }

    }





//      咨询部页面修改
    public function adviupdate(){
            $id = request()->param('id');
            $res=  Db::table('mdm_job')->where('job_id',$id)->find();
            $this->assign('res',$res);
            return $this->fetch('advisory/connoisseuradd');
    }
//咨询部页面提交修改数据
    public function update(){
        $id = request()->param();
        if ($id){
            $id['job_inter_time']=strtotime(request()->param('job_inter_time'));
            $id['job_order_time']=strtotime(request()->param('job_order_time'));
        }
        $result = new AdvisoryInfo();
        $row= $result->save($id,['job_id' => $id['job_id']]);
        if ($row){
            $this->success('修改成功','admin/advisory/advisory');
        }else{
            $this->error('修改失败','admin/advisory/adviupdate');
        }
    }
//    查看详情页面
  public function test(){
      $job_id = request()->param('job_id');
      $result =  Db::table('mdm_job')->where('job_id',$job_id)->find();
      $this->assign('result',$result);
      return $this->fetch('advisory/test');
}
//  查询所有咨询师当前月份接待人数
  public function monthfind(){
      $val = request()->param('month');
      $time = time();
      $nowmonth = strtotime(date("Y",$time).'-'.$val.'-01');
      $v =$val+1;
      if ($v>12){
          $v=1;
      }
      $nextmonth = strtotime(date("Y",$time).'-'.$v.'-01');
      $res = Db::table('mdm_user')->field('user_name')->where('user_r_id in(6,7)')->select();
//      拿取所有咨询师的名字
      $names=[];
      for ($i=0;$i<count($res);$i++){
          $names[]= $res[$i]['user_name'];
      }
//      得到时间段的job_id

//      联表去查个数
//      资讯次数
      $join_inter = 'u.user_name=j.job_inter_man and j.job_inter_time between '."'$nowmonth'".' and '."'$nextmonth'".'  and j.job_delete_time IS NULL';
//      var_dump($join);
      $inter_num = Db::table('mdm_user')->alias('u')
                ->field('u.user_name,COUNT(j.job_id) as init_num')
                ->join('mdm_job j',$join_inter,'left')
                ->where('u.user_r_id in(6,7)')
                ->group('u.user_name')
                ->select();
//      var_dump($inter_num);exit();
//      var_dump(Db::table('mdm_user')->getLastSql());
//     试岗次数s
      $join_try = 'u.user_name=j.job_inter_man and j.job_inter_time between '."'$nowmonth'".' and '."'$nextmonth'".' and j.job_try_sn=1  and j.job_delete_time IS NULL';
      $try_num = Db::table('mdm_user')->alias('u')
          ->field('u.user_name,COUNT(j.job_id) as try_num')
          ->join('mdm_job j',$join_try,'left')
          ->where('u.user_r_id in(6,7)')
          ->group('u.user_name')
          ->select();
//      var_dump(Db::table('mdm_user')->getLastSql());
//      签单
      $join_order = 'u.user_name=j.job_inter_man and j.job_order_time   between '."'$nowmonth'".' and '."'$nextmonth'".' and j.job_delete_time IS NULL';
      $order_num = Db::table('mdm_user')->alias('u')
          ->field('u.user_name,COUNT(j.job_id) as order_num')
          ->join('mdm_job j',$join_order,'left')
          ->where('u.user_r_id in(6,7)')
          ->group('u.user_name')
          ->select();
//      var_dump(Db::table('mdm_user')->getLastSql());
//var_dump($inter_num);s
//var_dump($try_num);
//var_dump($order_num);exit;
//      将拿到的数据组装成数组
      $inter = [];
      for($i = 0;$i < count($inter_num);$i++){
          $user_name = $inter_num[$i]['user_name'];
          $inter["$user_name"][] = $inter_num[$i];
          $inter["$user_name"][] = $try_num[$i];
          $inter["$user_name"][] = $order_num[$i];
      }
      $this->assign('inter',$inter);
//        var_dump($inter);
//      $timeFirst = strtotime(request()->param('start'));
//      $endTime = strtotime(request()->param('end'));
      $res = new AdvisoryInfo();
      if (!empty($nowmonth)&&!empty($nextmonth)) {
          $result = $res->whereTime('job_inter_time', 'between', [$nowmonth, $nextmonth])
              ->where('job_inter_man!=""')
              ->where('ISNULL(job_delete_time)')
              ->paginate(10);
      }else{
          $result = $res->select();
      }
      $this->assign('result',$result);
      return $this->fetch('advisory/connoisseur');
//      echo json_encode(['msg'=>200,'inter'=>$inter,'names'=>$names]);
//      if($inter){
//          echo json_encode(['msg'=>200,'inter'=>$inter,'names'=>$names]);
//      }else{
//          echo json_encode(['msg'=>300,'names'=>$names]);
//      }
  }


// 查询该咨询师当前月份接待人数
    public function permonthfind(){
        $r_name = $_SESSION['u_name'];
        $val = request()->param('month');
        $time = time();
        $nowmonth = strtotime(date("Y",$time).'-'.$val.'-01');
        $v =$val+1;
        if ($v>12){
            $v=1;
        }
        $nextmonth = strtotime(date("Y",$time).'-'.$v.'-01');
//     通过月份筛选
        $res = new AdvisoryInfo();
        if (!empty($nowmonth)&&!empty($nextmonth)) {
            $result = $res->whereTime('job_inter_time', 'between', [$nowmonth, $nextmonth])->where('job_inter_man',$r_name)->paginate(10);
        }else{
            $result = $res->where('job_inter_man',$r_name)->select();
        }
        $this->assign('result',$result);

//        资讯次数
        $join_inter = 'u.user_name=j.job_inter_man and j.job_inter_time between '."'$nowmonth'".' and '."'$nextmonth'".' and j.job_delete_time IS NULL';
        $inter_num = Db::table('mdm_user')->alias('u')
            ->field('u.user_name,COUNT(j.job_id) as init_num')
            ->join('mdm_job j',$join_inter,'left')
            ->where('u.user_r_id in(6,7)')
            ->where('u.user_name',$r_name)
            ->select();

        //     试岗次数s
        $join_try = 'u.user_name=j.job_inter_man and j.job_inter_time between '."'$nowmonth'".' and '."'$nextmonth'".' and j.job_try_sn=1  and j.job_delete_time IS NULL';
        $try_num = Db::table('mdm_user')->alias('u')
            ->field('u.user_name,COUNT(j.job_id) as try_num')
            ->join('mdm_job j',$join_try,'left')
            ->where('u.user_r_id in(6,7)')
            ->where('u.user_name',$r_name)
            ->select();
        //      签单
        $join_order = 'u.user_name=j.job_inter_man and j.job_order_time between '."'$nowmonth'".' and '."'$nextmonth'".' and j.job_delete_time IS NULL';
        $order_num = Db::table('mdm_user')->alias('u')
            ->field('u.user_name,COUNT(j.job_id) as order_num')
            ->join('mdm_job j',$join_order,'left')
            ->where('u.user_r_id in(6,7)')
            ->where('u.user_name',$r_name)
            ->select();
        // 将拿到的数据组装成数组
        $inter = [];
        for($i = 0;$i < count($inter_num);$i++){
            $inter[$i][] = $inter_num[$i];
            $inter[$i][] = $try_num[$i];
            $inter[$i][] = $order_num[$i];
        }
        $this->assign('inter',$inter);
        return $this->fetch('advisory/talk');
    }




    /**
     * @return mixed
     */
    //    咨询部人员管理页面
    public function person()
    {
        $res = new AdvisoryPerson();
        $result = $res->where("user_r_id in (6,7)")->select();
        $this->assign('result',$result);
        return $this->fetch('advisory/connoisseur2');
    }

    //      咨询部部门人员信息修改页面
    public function personupdate()
    {
        $user_id = request()->param('user_id');
        $res=  Db::table('mdm_user')->where('user_id',$user_id)->find();
        $this->assign('res',$res);
        return $this->fetch('advisory/connoisseurupdate');
    }
    //      咨询部修改部门人员
    public function personup(){
        $user_id = request()->param();
        $result = new AdvisoryPerson();
        $row= $result->save($user_id,['user_id' => $user_id['user_id']]);
        if ($row){
            $this->success('修改成功','admin/advisory/person');
        }else{
            $this->error('修改失败','admin/advisory/advisory');
        }
    }

    //      咨询部部门人员信息添加
    public function personadd()
    {
        return $this->fetch('advisory/connoisseur2add');
    }
    //      添加咨询部人员
    public function add()
    {
        $data = request()->param();
        if ($data){
            $data['user_nearname']=$data['user_name'];
            $data['user_password']=sha1(md5("z123456")."mingdaima");
            $data['user_r_id']=6;
        }
        $res = new AdvisoryPerson();
        $result = $res->allowField(true)->save($data);
        if ($result){
            $this->success('添加成功','admin/advisory/person');
        }
    }

    //人员删除效果
    public function ajax(){
        $user_id = request()->param('id');
        $res = Db::table('mdm_user')->where('user_id',$user_id)->delete();
        if ($res){
            echo json_encode(['msg'=>200]);
        }else{
            echo json_encode(['msg'=>300]);
        }
    }

    //启用停用状态
    public function ban(){
        $request = Request::instance();
        $id = $request->param('id');
//        echo $id;exit();
        $amininfo = new AdvisoryInfo();
        $status = $amininfo->where('job_id',$id)->value('job_getout_sn');
        if ($status == 0 ){
            $status = 2;
        }else{
            $status = 0;
        }
        $res = $amininfo->save(['job_getout_sn'=>$status],['job_id'=>$id]);
//        var_dump($res);exit();
        if($res){
            echo json_encode(['msg'=>200]);
        }else{
            echo json_encode(['msg'=>300]);
        }
    }
}
